﻿using System;
using System.Collections.Generic;
using System.Linq;
using Backend.JYD.Model;
using Backend.JYD.Common;

namespace Backend.JYD.EFDAL.DAL
{
	public class EmpRoleInfoDAL : Backend.JYD.IDAL.IEmpRoleInfo
	{
		#region 查询数据EmpRoleInfo


        public IList<EmpRoleInfo> GetAllEmpRoleInfo()
        {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.EmpRoleInfoes.Where(p => p.Status > 0);
                return query.ToList();
            }
        }

		/// <summary>
		/// 查询数据
		/// </summary>
		public PageInfo<EmpRoleInfo> GetEmpRoleInfoByPage(PageInfo page)
		{
			using (JydDataContext db = new JydDataContext())
			{
                IQueryable<EmpRoleInfo> query = db.EmpRoleInfoes.Where(p => p.Status > 0);
				//其他条件代码
				return new PageInfo<EmpRoleInfo>(page, query);
			}
		}

        public IList<EmpRoleInfo> GetEmpRoleInfoByEmpId(Int64 empId)
        {
            using (JydDataContext db = new JydDataContext())
            {
                IQueryable<EmpRoleInfo> query = db.EmpRoleInfoes.Where(p=>p.EmpId == empId && p.Status > 0);
                //其他条件代码
                return query == null ? null : query.ToList();
            }
        }

        public IList<EmpRoleInfo> GetEmpRoleInfoByRoleId(Int64 roleId)
        {
            using (JydDataContext db = new JydDataContext())
            {
                IQueryable<EmpRoleInfo> query = db.EmpRoleInfoes.Where(p => p.RoleId == roleId && p.Status > 0);
                //其他条件代码
                return query == null ? null : query.ToList();
            }
        }
        public IList<EmpRoleInfo> GetEmpRoleInfoByEmpIds(string empIds)
        {
            string[] eids = empIds.Split(new char[] { ',' });
            List<Int64> tempIds = null; eids.ToList().ForEach(p => { if (!string.IsNullOrWhiteSpace(p) && NumberHelper.StrToInt64(p) > 0) { tempIds.Add(NumberHelper.StrToInt64(p)); } });
            var ids = tempIds.ToArray();
            using (JydDataContext db = new JydDataContext())
            {
                IQueryable<EmpRoleInfo> query = db.EmpRoleInfoes.Where(p => ids.Contains(p.EmpId) && p.Status > 0);
                //其他条件代码
                return query == null ? null : query.ToList();
            }
        }

        public IList<EmpRoleInfo> GetEmpRoleInfoByRoleIds(string roleIds)
        {
            string[] eids = roleIds.Split(new char[] { ',' });
            List<Int64> tempIds = null; eids.ToList().ForEach(p => { if (!string.IsNullOrWhiteSpace(p) && NumberHelper.StrToInt64(p) > 0) { tempIds.Add(NumberHelper.StrToInt64(p)); } });
            var ids = tempIds.ToArray();
            using (JydDataContext db = new JydDataContext())
            {
                IQueryable<EmpRoleInfo> query = db.EmpRoleInfoes.Where(p => ids.Contains(p.RoleId) && p.Status > 0);
                //其他条件代码
                return query == null ? null : query.ToList();
            }
        }


		#endregion

		#region 插入数据

		/// <summary>
		/// 插入数据EmpRoleInfo
		/// </summary>
		public Int64 AddEmpRoleInfo(EmpRoleInfo info)
		{
			using (JydDataContext db = new JydDataContext())
			{
				db.EmpRoleInfoes.Add(info);
				db.SaveChanges();
				return info.EmpId;
			}
		}
		#endregion

		#region 修改数据EmpRoleInfo
        /// <summary>
        /// 根据字段（EmpId）修改数据EmpInfo
        /// </summary>
        public bool UpdateEmpRoleInfo(EmpRoleInfo info)
        {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.EmpRoleInfoes.Where(p => p.EmpId == info.EmpId && p.RoleId == info.RoleId);
                if (query != null)
                {
                    var temp = query.FirstOrDefault();
                    if (temp != null)
                    {
                        temp.Status = info.Status;
                        temp.Type = info.Type;
                        temp.OpId = info.OpId;
                        temp.UpdateTime = DateTime.Now;
                        //.......
                        return db.SaveChanges() > 0;
                    }
                }
                return false;
            }
        }
		#endregion

        public bool DeleteEmpRoleInfo(Int64 empId, Int64 roleId)
        {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.EmpRoleInfoes.Where(p => p.EmpId == empId && p.RoleId == roleId);
                if (query != null)
                {
                    db.Entry(query.FirstOrDefault()).State = System.Data.Entity.EntityState.Deleted;
                    return db.SaveChanges() > 0;
                }
                return true;
            }
        }
	}
}